package com.gz.moreaseManager.core.base;

import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.boot.system.ApplicationHome;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @文件名: BaseController
 * @版权:Copyright 2018-2020版权所有：ganz
 * @描述: 封装常用方法
 * @修改人: ganzheng
 * @修改时间: 9:55
 * @修改内容:新增
 */
public class BaseCommonUtils {


    /**
     * @Description: 将base64编码字符串转换为图片
     * @Author:
     * @CreateTime:
     * @param imgStr base64编码字符串
     * @param path 图片路径-具体到文件
     * @return
     */
    public static boolean generateImage(String imgStr, String path) {
        if (imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /***
    * @author :ganzheng
    * @Description: 根据base64生成文件返回路径
    * @date 14:14 2019/3/1
    * @return 
    */
    public static String base64ToFile( String base64 ) {
        File file = null;
        //获取项目路径
        ApplicationHome applicationHome = new ApplicationHome();
        //判断文件存储路径是否有文件
        File homePath = new File(applicationHome.getDir().getPath() + Constants.SYS_CONFIG_UPLOAD_PATH+"product/");
        if (!homePath.exists()) homePath.mkdir();
        //文件存放路径
        String filePath =applicationHome.getDir().getPath() + Constants.SYS_CONFIG_UPLOAD_PATH;

        String dataType = base64.substring(0,base64.indexOf(";"));
        String fileType ="jpg";
        if( dataType.contains("gif")){
            fileType="gif";
        }else if(dataType.contains("png")){
            fileType="png";
        }else if(dataType.contains("jpg")){
            fileType="jpg";
        }

        String imgStr =base64.substring(base64.indexOf(",")+1);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileType;
        //String fileName = UtilTool.getDateUUID()+'.'+fileType;

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(imgStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(homePath.getPath() + "/"+fileName);
            out.write(b);
            out.flush();
            out.close();
            return Constants.SYS_CONFIG_UPLOAD_URL + "product/"+fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
